package auto.app.controller;


import auto.app.controller.from.AccountPage;
import auto.app.controller.from.AccountRegisterFrom;
import auto.app.controller.from.account.GrantRoleAddFrom;
import auto.app.controller.info.AccountPageInfo;
import auto.app.controller.info.AccountRuleFrom;
import auto.app.controller.info.AccountRuleGetFrom;
import auto.app.controller.info.UserInfo;
import auto.app.exetion.AccountExetion.AccountSelect;
import auto.app.exetion.HibernateExetion;
import auto.app.exetion.MainExection;
import auto.app.model.AccountRule;
import auto.app.model.Rule;
import auto.app.service.AccountRuleService;
import auto.app.service.AccountService;
import auto.app.service.RuleService;
import auto.app.util.CommonUtil;
import auto.app.util.Result;
import auto.app.util.UserGetAccount;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-07-08
 */
@RestController
@RequestMapping("/account")
@Api(value = "用户管理",tags = "用户管理")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountRuleService accountRuleService;

    @Autowired
    private RuleService ruleService;

    @RequestMapping(value = "/loginerror",name = "未登录页面")
    @ApiOperation(value = "未登录页面",notes = "未登录页面")
    public Result<?>loginerror(){
        return Result.getInstance().setCode(500).setMsg("请您先登录");
    }

    @PostMapping(value = "/register",name = "注册")
    @ApiOperation(value = "注册",notes = "注册")
    public Result<Boolean> register(@Valid @RequestBody AccountRegisterFrom accountRegisterFrom) throws HibernateExetion, AccountSelect {
        //注册用户业务
        Boolean register = accountService.register(accountRegisterFrom);
        return Result.getInstance(Boolean.class).setData(register);
    }

    @PostMapping(value = "/findAllAccount",name = "用户列表")
    @ApiOperation(value = "用户列表",notes = "用户列表")
    public Result<AccountPageInfo>findAllAccount(@RequestBody AccountPage accountPage){
        AccountPageInfo allAccount = accountService.findAllAccount(accountPage);
        return Result.getInstance(AccountPageInfo.class).setData(allAccount);
    }

    @PostMapping(value = "/grantrole",name = "授予角色")
    @ApiOperation(value = "授予角色",notes = "授予角色")
    public Result<Boolean> grantRoleAdd(@Valid @RequestBody GrantRoleAddFrom grantRoleAddFrom) throws MainExection {
        Boolean aBoolean = accountService.grantRoleAdd(grantRoleAddFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    @GetMapping(value = "/getRuleAccount",name = "获取角色")
    @ApiOperation(value = "获取角色",notes = "获取角色")
    public Result<AccountRuleFrom> getRuleAccount(){
        //获取用户id
        Integer accountId = UserGetAccount.getAccountId();
        //查询账号的角色
        List<AccountRule> list = accountRuleService.list(new LambdaQueryWrapper<AccountRule>().eq(AccountRule::getAccountId, accountId));
        AccountRuleFrom build = AccountRuleFrom.builder().build();
        if (ObjectUtil.isNotNull(list)){
            build.setValue(list.get(0).getRuleId());
        }else{
            build.setValue(null);
        }
        //查询所有的校色
        ArrayList<AccountRuleGetFrom> accountRuleGetFroms=new ArrayList<>();
        List<Rule> rules = ruleService.list();
        for (Rule rule : rules) {
            AccountRuleGetFrom accountRuleGetFrom = AccountRuleGetFrom.builder().label(rule.getRuleName()).value(rule.getRuleId()).build();
            accountRuleGetFroms.add(accountRuleGetFrom);
        }
        build.setOptions(accountRuleGetFroms);
        return Result.getInstance(AccountRuleFrom.class).setData(build);

    }
    //测试是否返回用户id
    @GetMapping("/accountid")
    public Integer getId(){
        return UserGetAccount.getAccountId();
    }


    @GetMapping("/user/info")
    public Result<UserInfo> getInfo(){
        ArrayList<String> roles=new ArrayList<>();
        roles.add("admin");
        UserInfo admin = UserInfo.builder().name("admin").avatar(null).introduction(null).roles(roles).build();
        return Result.getInstance(UserInfo.class).setData(admin);
    }

}

